package cn.iocoder.yudao.module.etl.controller.admin.datasource;

import cn.iocoder.yudao.module.etl.dal.dataobject.DeDriver;
import cn.iocoder.yudao.module.etl.dal.dataobject.DeDriverDetails;
import cn.iocoder.yudao.module.etl.dto.DriverDTO;
import cn.iocoder.yudao.module.etl.service.datasource.DriverService;
import cn.iocoder.yudao.module.etl.utils.ResultHolder;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@ApiIgnore
@RequestMapping("etl/driver")
@RestController
public class DriverMgmController {

    @Resource
    private DriverService driverService;

    @ApiOperation("驱动列表")
    @PostMapping("/list")
    public ResultHolder listDeDriver() throws Exception{
        return ResultHolder.success(driverService.list());
    }

    @ApiOperation("删除驱动")
    @PostMapping("/delete")
    public void delete(@RequestBody DeDriver deDriver) throws Exception{
        driverService.delete(deDriver);
    }

    @ApiOperation("驱动列表")
    @GetMapping("/list/{type}")
    public ResultHolder listDeDriver(@PathVariable String type) throws Exception{
        List<DriverDTO> list = driverService.list();
        List<DriverDTO> collect = list.stream().filter(driverDTO -> driverDTO.getType().equalsIgnoreCase(type)).collect(Collectors.toList());
        return ResultHolder.success(collect);
    }

    @ApiOperation("添加驱动")
    @PostMapping("/save")
    public ResultHolder save(@RequestBody DeDriver deDriver) throws Exception{
        DeDriver save = driverService.save(deDriver);
        return ResultHolder.success(save);
    }

    @ApiOperation("更新驱动")
    @PostMapping("/update")
    public ResultHolder update(@RequestBody DeDriver deDriver) throws Exception{
        DeDriver update = driverService.update(deDriver);
        return ResultHolder.success(update);
    }

    @ApiOperation("驱动文件列表")
    @GetMapping("/listDriverDetails/{id}")
    public ResultHolder listDriverDetails(@PathVariable String id) throws Exception{
        List<DeDriverDetails> deDriverDetails = driverService.listDriverDetails(id);
        return ResultHolder.success(deDriverDetails);
    }

    @ApiOperation("删除驱动文件")
    @PostMapping("/deleteDriverFile")
    public void deleteDriverFile(@RequestBody DeDriverDetails deDriverDetails) throws Exception{
        driverService.deleteDriverFile(deDriverDetails.getId());
    }

    @ApiOperation("驱动文件上传")
    @PostMapping("file/upload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile"),
            @ApiImplicitParam(name = "id", value = "驱动D", required = true, dataType = "String")
    })
    public ResultHolder excelUpload(@RequestParam("id") String id, @RequestParam("file") MultipartFile file) throws Exception {
        DeDriverDetails deDriverDetails = driverService.saveJar(file, id);
        return ResultHolder.success(deDriverDetails);
    }



}
